package oylj.recycle.all_in_one.ic_card;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import java.util.Collection;

import usbrfidreader.ICReaderApi;

public class IcCardLib {


    private boolean isInitialized = false;
    private ICReaderApi api;
    private static final String TAG = "IcCardLib";
    public IcCardLib(Context context){
        this.initIcCardReader(context);
    }
    public boolean isInitialized() {
        return isInitialized;
    }

    public void  initIcCardReader(Context context){
        UsbManager manager = (UsbManager) context
                .getSystemService(Context.USB_SERVICE);
        Collection<UsbDevice> values = manager.getDeviceList().values();
        UsbDevice device = null;
        for (UsbDevice value : values) {
            int id = value.getProductId();
            int vendorId = value.getVendorId();
            //读卡器设备
            if (id == 53 || vendorId == 65535) {
                if (manager.hasPermission(value)) {
                    manager.openDevice(value);
                    device = value;
                    Toast.makeText(context, "设备已开启", Toast.LENGTH_SHORT).show();
                } else {
                    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent("com.android.example.USB_PERMISSION"), PendingIntent.FLAG_IMMUTABLE);
                    manager.requestPermission(value, mPermissionIntent);
                    Toast.makeText(context, "设备未找到", Toast.LENGTH_SHORT).show();
                }
            }
        }
        if (device != null) {
            api = new ICReaderApi(device, manager);
            isInitialized = true;
        }
    }

    private String showData(String text, byte[] data,  int pos,
                            int len) {
        StringBuilder dStr = new StringBuilder();
        for (int i = 0; i < len; i++) {
            dStr.append(String.format("%02x", data[i + pos]));
        }
        return dStr.toString().toUpperCase();
    }

    private String showStatue(String text, int Code) {
        String msg = null;
        switch (Code) {
            case 0x00:
                msg = "命令执行成功 .....";
                break;
            case 0x01:
                msg = "命令操作失败 .....";
                break;
            case 0x02:
                msg = "地址校验错误 .....";
                break;
            case 0x03:
                msg = "没有选择COM口 .....";
                break;
            case 0x04:
                msg = "读写器返回超时 .....";
                break;
            case 0x05:
                msg = "数据包流水号不正确 .....";
                break;
            case 0x07:
                msg = "接收异常 .....";
                break;
            case 0x0A:
                msg = "参数值超出范围 .....";
                break;
            case 0x80:
                msg = "参数设置成功 .....";
                break;
            case 0x81:
                msg = "参数设置失败 .....";
                break;
            case 0x82:
                msg = "通讯超时.....";
                break;
            case 0x83:
                msg = "卡不存在.....";
                break;
            case 0x84:
                msg = "接收卡数据出错.....";
                break;
            case 0x85:
                msg = "未知的错误.....";
                break;
            case 0x87:
                msg = "输入参数或者输入命令格式错误.....";
                break;
            case 0x89:
                msg = "输入的指令代码不存在.....";
                break;
            case 0x8A:
                msg = "在对于卡块初始化命令中出现错误.....";
                break;
            case 0x8B:
                msg = "在防冲突过程中得到错误的序列号.....";
                break;
            case 0x8C:
                msg = "密码认证没通过.....";
                break;
            case 0x8F:
                msg = "输入的指令代码不存在.....";
                break;
            case 0x90:
                msg = "卡不支持这个命令.....";
                break;
            case 0x91:
                msg = "命令格式有错误.....";
                break;
            case 0x92:
                msg = "在命令的FLAG参数中，不支持OPTION 模式.....";
                break;
            case 0x93:
                msg = "要操作的BLOCK不存在.....";
                break;
            case 0x94:
                msg = "要操作的对象已经别锁定，不能进行修改.....";
                break;
            case 0x95:
                msg = "锁定操作不成功.....";
                break;
            case 0x96:
                msg = "写操作不成功.....";
                break;
        }
        msg += '\n';
        text += msg;
        return text;
    }
    @JavascriptInterface
    public String readCardId(){
        String text = new String();
        byte mode= 0x52;
        byte halt = (byte) 0;
        byte[] snr = new byte[1];
        byte[] value = new byte[5]; // card number
        int result = api.GET_SNR(mode, halt, snr, value);
        text = showStatue(text, result);
        if (result == 0) {
//            if (snr[0] == 0x00)
//                text += ("只有一张卡.....\n");
//            else
//                text += ("多于一张卡......\n");
            text = showData(text, value,
                    0, 4);
        } else
            text = "";
        Log.d(TAG, "readCardId: "+text);
        return text;
//            text = showStatue(text, snr[0]);
    }

}
